package com.tarena.lbs.user.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.user.dao.mapper.UserMapper;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Repository
@RequiredArgsConstructor
public class UserRepositoryImpl implements UserRepository {

    private final UserMapper userMapper;

    @Override
    public List<UserVO> queryUserList(UserParam param) {
        QueryWrapper<UserPO> wrapper = new QueryWrapper<>();
        // 1. 模糊查询条件
        if (StringUtils.isNotBlank(param.getUserName())) {
            wrapper.like("user_name", param.getUserName());
        }
        if (StringUtils.isNotBlank(param.getNickName())) {
            wrapper.like("nick_name", param.getNickName());
        }
        if (StringUtils.isNotBlank(param.getPhone())) {
            wrapper.like("phone", param.getPhone());
        }
        // 2. 渠道来源精准查询（字符串转整数）
        if (StringUtils.isNotBlank(param.getProviderType())) {
                Integer providerType = Integer.parseInt(param.getProviderType());
                wrapper.eq("provider_type", providerType);

        }
        // 3. 注册时间范围查询（使用 between 条件）
        if (StringUtils.isNotBlank(param.getRegTimeStart()) && StringUtils.isNotBlank(param.getRegTimeEnd())) {
            wrapper.between("reg_time", param.getRegTimeStart(), param.getRegTimeEnd());
        } else if (StringUtils.isNotBlank(param.getRegTimeStart())) { // 仅起始时间
            wrapper.ge("reg_time", param.getRegTimeStart());
        } else if (StringUtils.isNotBlank(param.getRegTimeEnd())) { // 仅结束时间
            wrapper.le("reg_time", param.getRegTimeEnd());
        }
        // 查询数据并转换为VO
        List<UserPO> userList = userMapper.selectList(wrapper);
        return userList.stream().map(this::convertToVO).collect(Collectors.toList());
    }

    private UserVO convertToVO(UserPO po) {
        if (po == null) {
            return null;
        }
        UserVO vo = new UserVO();
        // 手动复制属性，确保字段正确映射
        vo.setId(po.getId());
        vo.setUserName(po.getUserName());
        vo.setPassword(po.getPassword());
        vo.setNickName(po.getNickName());
        vo.setIpAddress(po.getIpAddress());
        vo.setRegTime(po.getRegTime());
        vo.setLastLogTime(po.getLastLogTime());
        vo.setIntroduction(po.getIntroduction());
        vo.setPhone(po.getPhone());
        vo.setProviderType(po.getProviderType());
        vo.setStatus(po.getStatus());
        vo.setUnionId(po.getUnionId());
        vo.setUserNickPicture(po.getUserImage());
        return vo;
    }
}